﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Infrastructure.Base.Utils;

namespace Infrastructure.Mvc.SeedWork.Extensions
{
    public static class MvcHtmlExtensions
    {
        public static MvcHtmlString ActionDeleteLink(this HtmlHelper htmlHelper, string linkText, string actionName,
            string controllerName, object routeValues)
        {
            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues,
                new {@class = "DeleteAction"});
        }


        public static MvcHtmlString DropDownListForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            Type eEnum, bool isAllEmpty = false)
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            object value = null;
            if (htmlHelper.ViewData != null)
            {
                value = htmlHelper.ViewData.Eval(name);
            }

            List<SelectListItem> items = EnumHelper.GetValueAndDescriptions(eEnum).Select(item => new SelectListItem
            {
                Value = item.Key.ToString(CultureInfo.InvariantCulture),
                Text = item.Value,
                Selected = value != null && value.ToNullableString() == item.Value.ToNullableString()
            }).ToList();

            if (isAllEmpty)
            {
                items.Insert(0,
                    new SelectListItem
                    {
                        Text = "全部",
                        Value = string.Empty,
                        Selected = string.IsNullOrWhiteSpace(value.ToNullableString())
                    });
            }

            return htmlHelper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString TextBoxFor<TModel>(this HtmlHelper<TModel> htmlHelper,
            Expression<Func<TModel, decimal>> expression)
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            decimal value;
            if (htmlHelper.ViewData != null)
            {
                value = htmlHelper.ViewData.Eval(name) == null ? 0 : (decimal) htmlHelper.ViewData.Eval(name);
                return htmlHelper.TextBoxFor(expression, new {@Value = value.ToString("F2")});
            }
            return htmlHelper.TextBoxFor(expression, new {});
        }

        public static MvcHtmlString TextBox(this HtmlHelper htmlHelper, string name, decimal? value)
        {
            if (value.HasValue)
            {
                return htmlHelper.TextBox(name, value.Value.ToString("F2"));
            }
            return htmlHelper.TextBox(name);
        }

        public static MvcHtmlString ActionDeleteLink(this HtmlHelper htmlHelper, string linkText, string actionName,
            object routeValues)
        {
            return htmlHelper.ActionLink(linkText, actionName, routeValues, new {@class = "DeleteAction"});
        }


        public static MvcHtmlString DropDownListEnum<TModel, TProperty>(this HtmlHelper htmlHelper,
            Expression<Func<TModel, TProperty>> expression,
            Type eEnum, bool allowEmpty = true)
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            object value = null;
            if (htmlHelper.ViewData != null)
            {
                value = HttpContext.Current.Request[name] ?? string.Empty;
            }

            List<SelectListItem> items = EnumHelper.GetValueAndDescriptions(eEnum).Select(item => new SelectListItem
            {
                Value = item.Key.ToString(CultureInfo.InvariantCulture),
                Text = item.Value,
                Selected = value.ToString() == item.Value.ToString()
            }).ToList();

            if (allowEmpty)
            {
                items.Insert(0, new SelectListItem {Text = "全部", Selected = false, Value = ""});
            }

            return htmlHelper.DropDownList(name, items);
        }

        public static MvcHtmlString FixDefaultValueToEmpty<T>(this HtmlHelper htmlHelper, T value,
            string fixedString = "") where T : struct
        {
            string formatter = GetDefatultFormatString(typeof (T));
            if (default(T).Equals(value))
                return new MvcHtmlString(fixedString);

            string tmp = string.Format(formatter, value);
            string defalut = string.Format(formatter, default(T));
            if (tmp == defalut)
                return new MvcHtmlString(fixedString);
            return new MvcHtmlString(tmp);
        }

        private static string GetDefatultFormatString(Type type)
        {
            if (type == typeof (decimal))
                return "{0:f2}";
            return string.Empty;
        }
    }
}